﻿Imports EstoqueDAL

Public Class ComprasBLL
    Dim objDAL As ComprasDAL

#Region "Propriedades"
    Property com_codigo As Integer
    Property com_dtchegada As Nullable(Of DateTime)
    Property com_dtemissao As Nullable(Of DateTime)
    Property com_dtpedido As DateTime
    Property com_numnota As String
    Property com_valorfrete As Decimal
    Property com_valortotal As Decimal
    Property for_codigo As Integer
    Property sta_codigo As Integer
    Property litens As New List(Of ItensCompraBLL)
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(com_codigo As Integer, com_dtchegada As DateTime, com_dtemissao As DateTime, com_dtpedido As DateTime, com_numnota As String, com_valorfrete As Decimal, com_valortotal As Decimal, for_codigo As Integer, status As Integer)
        Me.com_codigo = com_codigo
        Me.com_dtchegada = com_dtchegada
        Me.com_dtemissao = com_dtemissao
        Me.com_dtpedido = com_dtpedido
        Me.com_numnota = com_numnota
        Me.com_valorfrete = com_valorfrete
        Me.com_valortotal = com_valortotal
        Me.for_codigo = for_codigo
        Me.sta_codigo = status
    End Sub
#End Region

#Region "Funções"
#Region "Inserir"
    Function inseriralterar(usuario As Integer) As Boolean
        Try
                objDAL = New ComprasDAL
                Me.com_codigo = objDAL.inseriralterar(Me.com_codigo, Me.com_dtchegada, Me.com_dtemissao, Me.com_dtpedido, Me.com_numnota, Me.com_valorfrete, Me.com_valortotal, Me.for_codigo, usuario, Me.sta_codigo)
                If (Me.com_codigo > 0) Then
                    excluiritens()
                    InserirItens()
                End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Excluir"
    Function excluir(usuario As Integer) As Boolean
        Try
            '4 aberto
            '6 fechado
            If (Me.sta_codigo <> 6) Then
                objDAL = New ComprasDAL
                Dim obj As New ItensCompraBLL
                obj.com_codigo = Me.com_codigo
                obj.excluir()
                objDAL.excluir(Me.com_codigo, usuario)
            Else
                Throw New Exception("Erro ao excluir compra")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Localizar"
    Public Function localizar(descricao As String, tipo As Integer, descricao2 As String) As DataTable
        Try
            objDAL = New ComprasDAL
            Return objDAL.buscar(descricao, tipo, descricao2)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        'If (com_valorfrete <= 0) Then Throw New Exception("Preencha o valor do frete")
        If (com_numnota.Trim = "") Then Throw New Exception("Preencha o numero da nota")
        If (com_valortotal <= 0) Then Throw New Exception("Preencha o valor total")
        validadatas()
        Return True
    End Function
#End Region

#Region "Valida datas"
    Public Function validadatas() As Boolean
        If (com_dtchegada Is Nothing) Then Throw New Exception("Preencha a data de chegada")
        If (com_dtemissao Is Nothing) Then Throw New Exception("Preencha a data de emissão")
        If (com_dtpedido.Date > com_dtemissao.Value.Date) Then Throw New Exception("A data de emissão não pode ser anterior a data de pedido")
        If (com_dtchegada.Value.Date < com_dtemissao.Value.Date) Then Throw New Exception("A data de  chegada não pode ser anterior a data de chegada")
        If (com_dtpedido.Date > com_dtchegada.Value.Date) Then Throw New Exception("A data de pedido não pode ser anterior a data de chegada")
        Return True
    End Function
#End Region

#Region "Buscar ItensCompra"
    Public Function buscaritens() As DataTable
        Dim tab As New DataTable
        Try
            Dim objitens As New ItensCompraBLL
            tab = objitens.localizar(Me.com_codigo, 1)
        Catch ex As Exception
            Throw New Exception("Erro ao localizar lista de itens")
        End Try
        Return tab

    End Function
#End Region
#End Region

#Region "Procedimentos"
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ComprasDAL
            tab = objDAL.buscar(codigo, 1, "")
            If (tab.Rows.Count > 0) Then
                Me.com_codigo = tab.Rows(0)("com_codigo").ToString

                If (tab.Rows(0)("com_dtemissao") Is DBNull.Value) Then
                    Me.com_dtchegada = Nothing
                Else
                    Me.com_dtemissao = tab.Rows(0)("com_dtemissao").ToString
                End If
                If (tab.Rows(0)("com_dtchegada") Is DBNull.Value) Then
                    Me.com_dtchegada = Nothing
                Else
                    Me.com_dtchegada = tab.Rows(0)("com_dtchegada").ToString
                End If
                Me.com_dtpedido = tab.Rows(0)("com_dtpedido").ToString

                Me.com_numnota = tab.Rows(0)("com_numnota").ToString
                Me.com_valorfrete = tab.Rows(0)("com_valorfrete").ToString
                Me.com_valortotal = tab.Rows(0)("com_valortotal").ToString
                Me.for_codigo = tab.Rows(0)("for_codigo").ToString
                Me.sta_codigo = tab.Rows(0)("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Inserir ItensCompra"
    Sub InserirItens()
        Try
            For Each item As ItensCompraBLL In litens
                item.com_codigo = Me.com_codigo
                item.inserir()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Excluir ItensCompra"
    Sub excluiritens()
        Dim item As New ItensCompraBLL
        item.com_codigo = Me.com_codigo
        item.excluir()
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.com_codigo = Nothing
        Me.com_dtchegada = Nothing
        Me.com_dtemissao = Nothing
        Me.com_dtpedido = Nothing
        Me.com_numnota = Nothing
        Me.com_valorfrete = Nothing
        Me.com_valortotal = Nothing
        Me.for_codigo = Nothing
        Me.sta_codigo = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
#End Region
End Class
